package in.hbb20.countrycodepickerproject.fragment;

import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.Context;

public abstract class Fragment implements Component.BindStateChangedListener {

    protected Context mContext;

    protected StackLayout mRootView;

    protected ChangeFragmentTool mChangeFragment;

    public void setChangeFragmentTool(ChangeFragmentTool changeFragment) {
        this.mChangeFragment = changeFragment;
    }

    public Fragment(Context context) {
        this.mContext = context;
    }

    protected abstract Component onCreateView(LayoutScatter layoutScatter, ComponentContainer container);


    protected abstract void onViewCreate();

    public Component findComponentById(int id) {
        return mRootView.findComponentById(id);
    }


    @Override
    public void onComponentBoundToWindow(Component component) {
        Component createView = onCreateView(LayoutScatter.getInstance(mContext), mRootView);
        mRootView.addComponent(createView);
        onViewCreate();
    }

    @Override
    public void onComponentUnboundFromWindow(Component component) {
        onDestroyView();
    }

    public Component attachToContainer(ComponentContainer componentContainer) {
        ComponentContainer.LayoutConfig layoutConfig = new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT);
        this.mRootView = new StackLayout(mContext);
        mRootView.setLayoutConfig(layoutConfig);
        mRootView.setBindStateChangedListener(this);
        componentContainer.addComponent(mRootView);
        return mRootView;
    }

    /**
     * onForeground
     *
     * @param intent intent
     */
    public void onForeground(Intent intent) {

    }

    /**
     * onBackground
     */
    public void onBackground() {

    }

    /**
     * onDestroyView
     */
    public void onDestroyView() {

    }

    public interface ChangeFragmentTool {
        /**
         * changeFragment
         *
         * @param index index
         */
        void changeFragment(int index);

        /**
         * getCurrentPage
         *
         * @return int
         */
        int getCurrentPage();
    }


}
